/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsistemaPaciente.programaMedico;

import clienteSandraParada.Login;
import java.awt.Color;

/**
 *
 * @author mario
 */
public class DialogoAgregarEstablecimiento extends javax.swing.JDialog {

    
    public DialogoAgregarEstablecimiento(java.awt.Frame parent, boolean modal, String rut) {
        super(parent, modal);
        initComponents();
        campoRut.getRut().setText(rut);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campoMensajeria = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        botonCancelar = new javax.swing.JButton();
        botonAgregar = new javax.swing.JButton();
        campoRut = new componentes.Rut();
        campoNombre = new javax.swing.JTextField();
        campoGrupoNivel = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        mensajeriaInformacion = new javax.swing.JLabel();
        campoMensajeria1 = new javax.swing.JLabel();

        campoMensajeria.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        campoMensajeria.setText("Area de mensajeria");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Rut establecimiento:");

        jLabel2.setText("Nombre establecimiento:");

        jLabel3.setText("Grupo nivel:");

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Agregar.png"))); // NOI18N
        botonAgregar.setText("Agregar");
        botonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarActionPerformed(evt);
            }
        });

        campoGrupoNivel.setModel(new javax.swing.SpinnerNumberModel(3, 1, 3, 1));

        jLabel4.setText("*");

        jLabel5.setText("*");

        mensajeriaInformacion.setText("Campos obligatorios (*)");

        campoMensajeria1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        campoMensajeria1.setText("Área de mensajeria");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(mensajeriaInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(botonAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCancelar)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(29, 29, 29)
                                        .addComponent(campoRut, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(campoGrupoNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                            .addComponent(campoNombre))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5))
                            .addComponent(campoMensajeria1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(campoRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoGrupoNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonCancelar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonAgregar)
                        .addComponent(mensajeriaInformacion)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoMensajeria1)
                .addGap(5, 5, 5))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarActionPerformed
        this.dispose();
        if(campoRut.isValido() && !campoGrupoNivel.equals("")){
            service.Establecimiento e = new service.Establecimiento();
            e.setRut(campoRut.getRut().getText());
            e.setNombre(campoNombre.getText());
            e.setGrupoNivel(Integer.parseInt(campoGrupoNivel.getValue().toString()));
            String valorNuevo="Rut establecimiento: "+e.getRut()+"\n"+"Nombre: "+e.getNombre()+"\n"+"Grupo nivel: "+e.getGrupoNivel();
            registrar(Login.getUser(), "Programa médico", "Agregó establecimiento: " + e.getRut(), "No aplica", valorNuevo);
            agregarEstablecimiento(e);
        }
        else{
            if(!campoRut.isValido())
                cambiarEtiquetaMensajeria("Debe ingresar un rut válido", Color.black);
            if(campoGrupoNivel.equals(""))
                cambiarEtiquetaMensajeria("Debe ingresar un grupo nivel", Color.black);
        }
    }//GEN-LAST:event_botonAgregarActionPerformed

    public void normalizarEtiquetaMensajeria(){
        campoMensajeria1.setText("Área de mensajería");
        campoMensajeria1.setForeground(Color.blue);
    }
   public void cambiarEtiquetaMensajeria(String mensaje, Color color){
        campoMensajeria1.setText(mensaje);
        campoMensajeria1.setForeground(color);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JSpinner campoGrupoNivel;
    private javax.swing.JLabel campoMensajeria;
    private javax.swing.JLabel campoMensajeria1;
    private javax.swing.JTextField campoNombre;
    private componentes.Rut campoRut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel mensajeriaInformacion;
    // End of variables declaration//GEN-END:variables

    private static boolean agregarEstablecimiento(service.Establecimiento establecimiento) {
        service.ProgramaFonasaWSService service = new service.ProgramaFonasaWSService();
        service.ProgramaFonasaWS port = service.getProgramaFonasaWSPort();
        return port.agregarEstablecimiento(establecimiento);
    }

    private static Long registrar(service.Usuario usuario, java.lang.String modulo, java.lang.String accion, java.lang.String valorAnterior, java.lang.String valorNuevo) {
        service.BitacoraWSService service = new service.BitacoraWSService();
        service.BitacoraWS port = service.getBitacoraWSPort();
        return port.registrar(usuario, modulo, accion, valorAnterior, valorNuevo);
    }

}
